package com.mapull.list;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 *
 * List 中随机获取数据
 * @author mapull
 */
public class RandomList {
    private static List<String> list = Arrays.asList("A", "B", "C", "D", "E", "F", "E", "D", "E");

    public static void main(String[] args) {
        randomLettersSf(list);
    }
    public static void randomLetter(List<String> list){
        Random random = new Random();
        String letter = list.get(random.nextInt(list.size()));
        System.out.println(letter);
    }

    public static void randomLetter2(List<String> list){
        int index = ThreadLocalRandom.current().nextInt(list.size());
        String s = list.get(index);
        System.out.println(s);
    }

    public static void randomLetters(List<String> list){
        List<String> results = new ArrayList<>();
        Random random = new Random();

        // 获取 3 个随机元素
        int count = 3;
        for(int i =0; i< count;i++){
            String letter = list.get(random.nextInt(list.size()));
            results.add(letter);
        }
        System.out.println(results);
    }

    public static void randomLettersSub(List<String> list){
        Random random = new Random();

        // 获取 3 个随机元素
        int count = 3;
        int end = random.nextInt(list.size() - count) + count;
        List<String> results = list.subList(end - count, end);
        System.out.println(results);
    }

    public static void randomLettersSf(List<String> list){
        Collections.shuffle(list);
        // 获取 3 个随机元素
        int count = 3;
        List<String> results = list.subList(0, count);
        System.out.println(results);
    }
}
